package two.chapter_1.c1_3;

import java.util.Iterator;

/**
 * 双端队列   双向链表实现
 * @param <Item>
 */
public class DoubleLinkedDeque<Item> implements Deque<Item> {
    private int size;
    private Node left;//队列最左端
    private Node right;//队列最右端
    @Override
    public void pushLeft(Item item) {
        Node node=new Node(item);
        if (isEmpty()){
            left=right=node;
        }else {
            left.prev=node;
            node.next=left;
            left=node;
        }
        size++;
    }

    @Override
    public void pushRight(Item item) {
        Node node=new Node(item);
        if (isEmpty()){
            left=right=node;
        }else {
            right.next=node;
            node.prev=right;
            right=node;
        }
        size++;
    }

    @Override
    public Item popLeft() {
        return null;
    }

    @Override
    public Item popRight() {
        return null;
    }

    @Override
    public boolean isEmpty() {
        return size()==0;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public Iterator<Item> iterator() {
        return new Iter();
    }

    private class Node{
        Item item;
        Node prev;
        Node next;
        public Node(Item item){
            this.item=item;
        }
    }

    private class Iter implements Iterator<Item>{
        private Node prev=right;
        @Override
        public boolean hasNext() {
            return prev!=null;
        }

        @Override
        public Item next() {
            Item item=prev.item;
            prev=prev.prev;
            return item;
        }

        @Override
        public void remove() {

        }
    }
}
